<template>
	<view class="box">
		<view class="searchwrap">
			<view class="searchwrapleft">
				<image class="search" :src="baseUrl + '/static/icon/i_search.png'" mode="aspectFill"></image>
				<input type="text" class="ipt" placeholder="搜索城市/区县"  v-model ="words" />
			</view>
			<view class="searchwrapright">
				<view class="line">
					
				</view>
				<view class="sear">
					搜索
				</view>
			</view>
		</view>
		<view class="twomain" v-if="words.length==0" >
			<view class="twomainleft">
				<image :src="baseUrl + '/static/icon/i_dw.png'" mode="aspectFill"></image>
				<text class="">
					当前位置：{{cityName}}
				</text>
			</view>
			<view class="twomainright" @click="gets()">
				重新定位
			</view>
		</view>
		<view class="main" v-if="words.length>0"  v-for="item in list" :key="item.id" @click="selcity(item)" >
			<view class="name">
				{{item.title}}
			</view>
			<view class="content">
				{{item.address}}
			</view>
		</view>
		<view class="tjwrap" v-if="words.length==0" >
			<view class="tit">
				<text>城市/区县</text>
				<view class="right" @click="gonext">
					
					全部 <image :src="baseUrl + '/static/icon/right.png'" mode="aspectFill"></image>
				</view>
			</view>
			<view class="btnwrap">
				<view class="btnitem" v-for="item in cityList" @click="changecity(item)" >
					{{item.name}}
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		area
	} from "../../api/user.js";
	import {
		geocoder
	} from "../../api/other.js"
	import {
		searchByKeyword,
		hotCity
	} from "../../api/local.js"
	export default {
		data(){
			return{
				baseUrl: this.$imageBaseUrl,
				words:"",
				arealist: [],
				flag:false,
				full_location:"",
				fieldMapping: {
					text: 'name', // 将 name 映射到 label
					value: 'id',
					// 将 code 映射到 value
				},
				latitude:"",
				longitude:"",
				cityName:"",
				list:[],
				cityList:[]
			}
		},
		onLoad() {
			if(uni.getStorageSync("cityName")){
				this.cityName=uni.getStorageSync("cityName")
			}
			this.getcity()
		},
		watch: {
			words(newVal, oldVal) {
				this.search()
			},
		},
		methods:{
			gonext(){
				uni.navigateTo({
					url:"/pagesMy/groundCat/selprovince"
				})
			},
			changecity(item){
				uni.setStorageSync("latitude",item.latitude)
				uni.setStorageSync("longitude",item.longitude)
				uni.setStorageSync("cityName",item.name)
				setTimeout(()=>{
					uni.switchTab({
						url:"/pages/index/groundCat"
					})
				
				},500)
				uni.$u.toast("选择成功")
			},
			getcity(){
				hotCity().then(res=>{
					this.cityList=res.data
				})
			},
			selcity(item){
				console.log(item);
				uni.setStorageSync("latitude",item.location.lat)
				uni.setStorageSync("longitude",item.location.lng)
				uni.setStorageSync("cityName",item.district)
				setTimeout(()=>{
					uni.switchTab({
						url:"/pages/index/groundCat"
					})
				
				},500)
				uni.$u.toast("选择成功")
			},
			search(){
				searchByKeyword({
					keyword:this.words,
					
				}).then(res=>{
					this.list=res.data
				})
			},
			gets(){
				let that = this;
				uni.getLocation({
					success: function(res) {
						uni.setStorageSync("latitude", res.latitude)
						uni.setStorageSync("longitude", res.longitude)
						that.latitude = res.latitude
						that.longitude = res.longitude
					
						console.log(res);
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						let data = {
							lat: res.latitude,
							lng: res.longitude,
						};
						geocoder(data).then(res => {
							console.log('逆解析', res)
							if (res.code == 1) {
								that.cityName = res.data.city
								console.log(that.cityName);
								uni.setStorageSync('cityName', res.data.district)
								uni.setStorageSync('addressInfo', res.data)
								uni.switchTab({
									url:"/pages/index/groundCat"
								})
							}
							uni.$u.toast("定位成功")
						})
					},
					fail: function(res) {
						that.latitude="39.90"
						that.longitude="116.41"
						that.cityName = "全国"
						uni.setStorageSync("latitude", that.latitude)
						uni.setStorageSync("longitude", that.longitude)
						uni.switchTab({
							url:"/pages/index/groundCat"
						})
						console.log(res, "杀杀杀杀杀杀杀杀杀")
					}
				})
			},
		}
	}
</script>
<style>
	page{
		background: #FFFFFF;
	}
</style>
<style scoped lang="scss">
	.box{
		width: 750rpx;
	}
	.main{
		width: 690rpx;
		height: 118rpx;
		margin: 0 auto;
		border-bottom: 2rpx solid #F2F2F2;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}
	
	.content{
		font-weight: 400;
		font-size: 24rpx;
		color: #999999;
	}
	.name{
		font-weight: 400;
		font-size: 30rpx;
		color: #333333;
	}
	.twomain{
		
		width: 690rpx;
		margin: 0 auto;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.twomainleft{
		display: flex;
		align-items: center;
		>image{
			width: 22rpx;
			height: 28rpx;
		}
		>text{
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;
		}
	}
	.twomainright{
		font-weight: 400;
		font-size: 28rpx;
		color: #FFC300;
	}
	.searchwrap{
		background: #F7F7F7;
		width: 690rpx;
		height: 72rpx;
		margin: 0 auto;
		border-radius: 110rpx 110rpx 110rpx 110rpx;
		display: flex;
	}
	.searchwrapleft{
		width: 570rpx;
		display: flex;
		align-items: center;
		margin-left: 22rpx;
	}
	.search{
		width: 28rpx;
		height: 28rpx;
	}
	.ipt{
		padding-left: 16rpx;
		font-size: 24rpx;
		color: #999999;
		/* #ifdef MP-ALIPAY */
		background: transparent;
		/* #endif */
		
	}
	.searchwrapright{
		display: flex;
		align-items: center;
	}
	.line{
		width: 2rpx;
		height: 36rpx;
		background: #D8D8D8;
	}
	.sear{
		font-size: 28rpx;
		color: #FFC300;
		margin-left: 10rpx;
	}
	.tjwrap{
		width: 690rpx;
		max-height: 342rpx;
		background: #F7F7F7;
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		margin:  0 auto;
		margin-top: 30rpx;
	}
	
	.tit{
		position: relative;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-top: 20rpx;
		>text{
			font-weight: 400;
			font-size: 26rpx;
			color: #999999;
			margin-left: 20rpx;
		}
		.right{
			position: relative;
			>text{
				position: absolute;
				right: 20rpx;
				top: 20rpx;
			}
			font-weight: 400;
			font-size: 26rpx;
			color: #999999;
			>image{
				width: 14rpx;
				height: 20rpx;
				margin-right: 20rpx;
				margin-left: 10rpx;
			}
		}
	}
	.btnwrap{
		width: 600rpx;
		display: flex;
		flex-wrap: wrap;
		.btnitem{
			min-width: 110rpx;
			height: 56rpx;
			background: #FFFFFF;
			border-radius: 100rpx 100rpx 100rpx 100rpx;
			text-align: center;
			line-height: 56rpx;
			font-weight: 400;
			font-size: 28rpx;
			color: #3D3D3D;
			margin: 10rpx;
			// padding: 8rpx;
			
		}
	}
</style>